package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.StudentGroups;
import com.eece496.Helpers.GroupInfo;

/**
 * Servlet implementation class ProfAddGroupManualServlet
 */
public class ProfAddGroupManualServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfAddGroupManualServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String size = request.getParameter("size");
		int length = Integer.parseInt(size);
		ArrayList<String> groupNumber = new ArrayList<String>();
		ArrayList<Integer> studentIDs = new ArrayList<Integer>();
		ArrayList<String> divisions = new ArrayList<String>();
		ArrayList<String> classrooms = new ArrayList<String>();
		
		StudentGroups StudentGroupDB = new StudentGroups();
		StudentGroupDB.Setup(connection);
		
		String Message = "Success Add";
		
		for(int i =0 ;i< length; i++)
		{
			//check if the groupNumbers are numbers and NOT letters!!
			//if (Pattern.matches("[a-zA-Z]+", request.getParameter("groupNumber"+i)) == false){
				
				studentIDs.add(Integer.parseInt(request.getParameter("studentID"+i)));
				groupNumber.add(request.getParameter("groupNumber"+i));
				divisions.add(request.getParameter("division"+i));
				classrooms.add(request.getParameter("classroom"+i));
		//	}
			//else
			//{
			//	Message = "Failed";
			//	break;
			//}
		}
		
		/*
		if(Message.equals("Failed"))
		{
			ArrayList<GroupInfo> Info = new ArrayList<GroupInfo>();

			try {
				Info = StudentGroupDB.GetAllGroups(courseNumber, sectionNumber);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("GroupsInfo", Info);
			request.setAttribute("Message", Message);
			request.getRequestDispatcher(
					"/Professor/Professor-View-Groups.jsp").forward(
					request, response);
		}
		*/
		//else{
		ArrayList<GroupInfo> gInfo = new ArrayList<GroupInfo>();
		ArrayList<GroupInfo> Info = new ArrayList<GroupInfo>();
		try {
			for(int i =0; i< studentIDs.size(); i++)
			{
				StudentGroups sGroup = new StudentGroups(groupNumber.get(i),divisions.get(i), studentIDs.get(i), courseNumber, sectionNumber, classrooms.get(i),1);
				sGroup.Setup(connection);
				sGroup.Insert();
			}
			
				Info = StudentGroupDB.GetAllGroups(courseNumber, sectionNumber);

		//Get all students that aren't grouped up
		ArrayList<GroupInfo> unGroupedInfo = new ArrayList<GroupInfo>();

		unGroupedInfo = StudentGroupDB.GetUnGroupedStudents(courseNumber, sectionNumber);
		int count =0;
		for(int i=0; i< unGroupedInfo.size(); i++)
		{
			if(StudentGroupDB.UserExists(courseNumber, sectionNumber, unGroupedInfo.get(i).studentID)==false)
			{
				gInfo.add(unGroupedInfo.get(i));
				System.out.println(count);
			}
		}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("unGroupedInfo", gInfo);
			request.setAttribute("GroupsInfo", Info);
			request.setAttribute("Message", Message);
			request.getRequestDispatcher(
					"/Professor/Professor-View-Groups.jsp").forward(
					request, response);
		//}	
	}
}
